//
//  LOLListViewController.m
//  LOLXXbox
//
//  Created by dllo on 16/2/29.
//  Copyright © 2016年 Grasstrio. All rights reserved.
//

#import "LOLListViewController.h"
#import "LOLListTableViewcell.h"
#import "LSZNetWorkTools.h"
#import "LOLNewmodel.h"
#import "LOLMvPlayerViewController.h"
@interface LOLListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation LOLListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creattableView];
    [self getData];
}

- (void)creattableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 145) style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[LOLListTableViewcell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableView];
    self.dataSource = [NSMutableArray array];
}

- (void)getData{
    [LSZNetWorkTools getWithUrl:@"http://box.dwstatic.com/apiVideoesNormalDuowan.php?src=duowan&action=l&sk=&pageUrl=&heroEnName=&tag=jingjieveryday&p=1" parameter:nil httpHeader:nil resposeType:ResponseTypeJSON success:^(id result) {
        NSArray *array = result;
        for (NSDictionary *dic in array) {
            LOLNewmodel *model = [[LOLNewmodel alloc] initWithDataSource:dic];
            [self.dataSource addObject:model];
   
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } fail:^(NSError *error) {
        
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LOLListTableViewcell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.myNewModel = self.dataSource[indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LOLMvPlayerViewController *mvPlayer = [[LOLMvPlayerViewController alloc] init];
    LOLNewmodel *model = self.dataSource[indexPath.row];
    
    [LSZNetWorkTools getWithUrl:[NSString stringWithFormat:@"http://box.dwstatic.com/apiVideoesNormalDuowan.php?action=f&vid=%@", model.vid] parameter:nil httpHeader:nil resposeType:ResponseTypeJSON success:^(id result) {
        NSURL *url = [NSURL URLWithString:result[@"result"][@"items"][@"350"][@"transcode"][@"urls"][0]];
        
        mvPlayer.play_url = [NSString stringWithFormat:@"%@", url];
        
        [self presentViewController:mvPlayer animated:YES completion:^{
            
        }];
        
    } fail:^(NSError *error) {
        
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
